//
//  JLSMainViewController.m
//  看点
//
//  Created by 焦林生 on 15/12/15.
//  Copyright © 2015年 jiaolinsheng. All rights reserved.
//

#import "JLSMainViewController.h"
#import "JLSChannel.h"
#import "JLSHeadlineViewController.h"
#import "JLSSocietyViewController.h"
#import "JLSHomeLabel.h"
#import "JLSLeftDockMenu.h"

@interface JLSMainViewController ()<UIScrollViewDelegate>
/**
 *  存放标题
 */
@property (weak, nonatomic) IBOutlet UIScrollView *titleScrollView;
/**
 *  内容
 */
@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;

/**
 *  存放频道清单的数组属性
 */
@property (nonatomic, strong) NSArray *channalList;

/**
 *  左边菜单视图
 */
@property (nonatomic, weak) JLSLeftDockMenu *leftDockMenu;
@end

@implementation JLSMainViewController

//懒加载
- (NSArray *)channalList
{

    if (_channalList == nil) {
        
        //1 加载 json 的二进制数据
        NSString *path = [[NSBundle mainBundle] pathForResource:@"topic_news.json" ofType:nil];
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        //2 反序列化
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:NULL];
        
        //3 从字典中获取数据'
        NSArray *dictArray = dict[dict.keyEnumerator.nextObject];
        
        //4 进行字典转模型
        NSMutableArray *Marray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            JLSChannel *channel = [JLSChannel channelWithDict:dict];
            [Marray addObject:channel];
        }
        
        //5 返回一个从小到大的数组,tid 是从小到大排列的
        _channalList = [Marray sortedArrayUsingComparator:^NSComparisonResult(JLSChannel *obj1, JLSChannel *obj2) {
            return [obj1.tid compare:obj2.tid];
        }];
    }
    return _channalList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    //1 添加所有的子控制器
    [self setupChildVC];
    
    //2 添加所有的顶部标题
    [self setupTitles];
    
    //3 设置 scrollView的一些属性
    [self setupScrollViewProperties];
    
    //4 添加默认自控制器
    [self setupDefaultViewController];
    
    //5 设置默认 JLSHomeLabel的比例值
    JLSHomeLabel *firstLabel = [self.titleScrollView.subviews firstObject];
    firstLabel.scale = 1.0;
    
    //6 设置导航栏左边的item
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sidebar_nav_news"] style:UIBarButtonItemStylePlain target:self action:@selector(navLeftItemClick)];
    
}

#pragma mark - 导航栏左边 item 点击事件
- (void)navLeftItemClick
{

    //1 如果左边的视图已经存在,判断展开还是关闭
    if (self.leftDockMenu) {
        //左边菜单视图为展开状态
        if (self.contentScrollView.contentOffset.x < 0) {
            [self.contentScrollView setContentOffset:CGPointZero animated:YES];
        } else {
        
            //让内容滚动视图向右偏移200间距,特别注意:一旦视图被点击或者触摸,
            //就会执行 scrollViewDidScroll: 方法,恢复原来的状态
            [self.contentScrollView setContentOffset:CGPointMake(-200, 0) animated:YES];
        }
    } else {
    
        //2 若果左视图不存在,创建左视图
        [self setupLeftDockMenu];
        [self.contentScrollView setContentOffset:CGPointMake(-200, 0) animated:YES];
    }
}

#pragma mark - 添加左菜单右菜单
- (void)setupLeftDockMenu
{

    //1 创建左边的菜单视图
    JLSLeftDockMenu *leftDockMenu = [[JLSLeftDockMenu alloc] init];
    leftDockMenu.height = 300;
    leftDockMenu.width = 150;
    leftDockMenu.x = -200;
    leftDockMenu.y = (self.contentScrollView.height - leftDockMenu.height) * 0.5;
    
    //2 添加到滚动视图中
    [self.contentScrollView addSubview:leftDockMenu];
    
    //3 记录左边的菜单视图
    self.leftDockMenu = leftDockMenu;
}

#pragma mark - 添加默认子控制器
- (void)setupDefaultViewController
{

    JLSHeadlineViewController *firstVc = (JLSHeadlineViewController *)[self.childViewControllers firstObject];
    firstVc.view.frame = self.contentScrollView.bounds;
    [self.contentScrollView addSubview:firstVc.view];
}

#pragma mark - 设置 scrollVeiw的一些属性
- (void)setupScrollViewProperties
{

    //1 取消水平和垂直滚动条,
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    self.titleScrollView.showsVerticalScrollIndicator  = NO;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.showsVerticalScrollIndicator = NO;
    
    //2 设置 scrollView的代理方法,
    self.contentScrollView.delegate = self;
    
    //3 设置分页效果
    self.contentScrollView.pagingEnabled = YES;
    
    //4 设置内容滚动视图size
    CGFloat contentW = self.childViewControllers.count * [UIScreen mainScreen].bounds.size.width;
    self.contentScrollView.contentSize = CGSizeMake(contentW, 0);
    //5 取消 scrollView自动调整内边距的属性值
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

#pragma mark - 添加所有的子控制器
- (void)setupChildVC
{

    //1 新闻控制器
    UIStoryboard *headlineSB = [UIStoryboard storyboardWithName:@"Headline" bundle:nil];
    
    JLSHeadlineViewController *headVC = [headlineSB instantiateViewControllerWithIdentifier:@"Headline"];
    
    //传递 URL 字符串 ->用来发送请求
    headVC.urlString = [self.channalList[0] tid];
    
    //传递标题名称 -> 用来显示在 label标签上
    headVC.title = [self.channalList[0] tname];
    
    //添加自控制器
    [self addChildViewController:headVC];
    
    //2 社会控制器
    UIStoryboard *societySB = [UIStoryboard storyboardWithName:@"Society" bundle:nil];
    JLSSocietyViewController *societyVc = [societySB instantiateViewControllerWithIdentifier:@"Society"];
    societyVc.urlString = [self.channalList[1] tid];
    societyVc.title = [self.channalList[1] tname];
    [self addChildViewController:societyVc];
    
    //3 循环创建多个控制器
    int count = (int)self.channalList.count - 2;
    
    for (int i =0; i < count; i++) {
        
        UIViewController *vc = [[UIViewController alloc] init];
        vc.title = [self.channalList[i + 2] tname];
        [self addChildViewController:vc];
    }
    
}

#pragma mark 添加顶部的所有标题
- (void)setupTitles
{

   //创建 label
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat w = 80;
    CGFloat h = 30;
    CGFloat y = 0;
    for (NSUInteger i = 0; i < count; i++) {
        //
        JLSHomeLabel *label = [[JLSHomeLabel alloc] init];
        label.tag = i;
        [self.titleScrollView addSubview:label];
        
        //设置 frame
        CGFloat x = i * w;
        label.frame = CGRectMake(x, y, w, h);
        
        // 设置文字
        UIViewController *vc = self.childViewControllers[i];
        label.text = vc.title;
        
        //监听点击
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)]];
    }
    
    //设置 scrollView的内容大小
    CGFloat titlesContentW = count * w;
    self.titleScrollView.contentSize = CGSizeMake(titlesContentW, 0);
}
/**
 *   监听 label 的点击
 */
- (void)labelClick:(UITapGestureRecognizer *)recognizer
{

    //1 获得被点击的 label
    JLSHomeLabel *label = (JLSHomeLabel *)recognizer.view;
    
    //2 计算 x 方向上的偏移量
    CGFloat offsetX = label.tag  * self.contentScrollView.frame.size.width;
    
    //3 设置偏移量
    [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}




@end
